% This file generates the IRF presented in second quadrant (1,2) in Figure 
% 9 in the paper. It needs to be ran from 'FIG_9_Robustness_Output.m'.

global P2 lambda P H PP
%% Options for the specific results to generate.

graph           =   0;          % = 1 to generate a plot. 

csv_file        =   1;          % = 1 if you want to import from CSV.
                                % = 0 if you want to import from XLSX.

use_data        =   1;          % = 1   if baseline data.                  

%% Importing and Preparing the data.

if csv_file == 1
    data            =   csvread('Data_File_Growth_Specification.csv');
    % UM Expectations Median:     
    pilev_data      =   csvread('Data_File_Inflation.csv');
    pilev           =   pilev_data(:,6);     
else
    data            =   xlsread('Data_File.xlsx','Growth Specification');
    % UM Expectations Median:
    pilev           =   xlsread('Data_File.xlsx','Inflation','F2:F281');
end
% Import the specific variables. 
dz              =   data(:,3);
dy              =   data(:,4);
zlb             =   data(:,5);
rec             =   data(:,6);
zlev            =   data(:,7);
time            =   data(:,9);
ylev            =   data(:,8);      
[T,n]           =   size(dy);
    
% Defining the sample:
st              =   148;                    % = 1 for full sample 
                                            % = 148 for 1984Q1 
                                            % = 52 for 1960Q1. 

                                            %% Options for local projections.
h1              =   1;      % For splines.        
lambda          =   100;    % Value of penalty of SLP.
r               =   3;      % r-1 = order of the limit polynomial for the SLP.
PP              =   P;                               

%% Creating variables.

if use_data > 1
    dy          =   [0;diff(ylev)];
end

% LHS:
ylev3           =   ylev(st:end);
% Output growth:
ylev2           =   dy(st:end);
% Productivity:
zlev2           =   dz(st:end);
% Inflation:
pilev2          =   pilev(st:end);
% Dummy for ZLB:
zlb2            =   zlb(st:end);

% Creating W matrix: 
ff              =   [ylev2 zlev2 pilev2];
w               =   lagmatrix(ff,1:P);          % Remove period t observations.

% Creating lagged variables interacted with period-t ZLB dummy:
[TY,YT]         =   size(w);
zlb3            =   zeros(TY,YT);
for jx = 1:YT
    zlb3(:,jx)  =   zlb2;
end
www             =   zlb3.*w; 
zlb4            =   1- zlb3;
wwww            =   zlb4.*w;

%% Creating the IRFs.

% LHS variable:   
y                   =   ylev3(1+P:end,:); 

% Local projection.
        % Standard case. 
w                       =   [zlb2(1+P:end) zlb2(1+P:end).*zlev2(1+P:end),...
                             wwww(1+P:end,:), www(1+P:end,:)]; 
x                       =   (1-zlb2(1+P:end)).*zlev2(1+P:end,:);  % Shock variable. 
lp                      =   locproj_var_growth(y,x,w,h1,H,ylev3,'reg'); 
% Extract the IRF from local projection: 
irf                     =   lp.IR(2:H+1); 
% Construct standard errors:
u                       =   lp.Y - lp.X*lp.theta;
[nwse_lp,vc_lp]         =   NeweyWest(u,lp.X,0,0);
        % ZLB case.
w                       =   [zlb2(1+P:end) (1-zlb2(1+P:end)).*zlev2(1+P:end,:),...
                             wwww(1+P:end,:), www(1+P:end,:)];
x                       =   zlb2(1+P:end).*zlev2(1+P:end);
lp_zlb                  =   locproj_var_growth(y,x,w,h1,H,ylev3,'reg'); 
% Extract the IRF from local projection: 
irf_zlb                 =   lp_zlb.IR(2:H+1);
% Construct standard errors:
u                       =   lp_zlb.Y - lp_zlb.X*lp_zlb.theta;
[nwse_lp_zlb,vc_lp_zlb] =   NeweyWest(u,lp_zlb.X,0,0);

% Smooth local projection.
        % Standard case. 
w                       =   [zlb2(1+P:end) zlb2(1+P:end).*zlev2(1+P:end),...
                             wwww(1+P:end,:), www(1+P:end,:)];
x                       =   (1-zlb2(1+P:end)).*zlev2(1+P:end,:);    
slp                     =   locproj_var_growth(y,x,w,h1,H,ylev3,'smooth',r,lambda);
% Extract the IRF from local projection: 
irf_slp                 =   slp.IR(2:H+1);
P2                      =   slp.P2;
% Construct standard errors:
u                       =   slp.Y - slp.X*slp.theta;
[nwse_slp,vc_slp,Q]     =   NeweyWest_slp(u,slp.X,0,0);
V2                      =   slp.B*vc_slp(1:slp.K,1:slp.K)*slp.B';
se_slp                  =   sqrt(diag(V2));
        % ZLB case.
w                       =   [zlb2(1+P:end) (1-zlb2(1+P:end)).*zlev2(1+P:end),...
                             wwww(1+P:end,:), www(1+P:end,:)];
x                       =   zlev2(1+P:end,:).*zlb2(1+P:end);
slp_zlb                 =   locproj_var_growth(y,x,w,h1,H,ylev3,'smooth',r,lambda); 
% Extract the IRF from local projection: 
irf_slp_zlb             =   slp_zlb.IR(2:H+1);
% Construct standard errors:
u                       =   slp_zlb.Y - slp_zlb.X*slp_zlb.theta;
[nwse_slp_zlb,vc_slp_zlb] = NeweyWest_slp(u,slp_zlb.X,0,0);
V22                     =   slp_zlb.B*vc_slp_zlb(1:slp_zlb.K,1:slp_zlb.K)*slp_zlb.B';
se_slp_zlb              =   sqrt(diag(V22));

%% Saving results to Excel.
p                       =   0.9;
bb                      =   tinv(p,TY-P);

irf_lp_baseline_LB      =   (irf-bb*nwse_lp(1:H));
irf_lp_baseline_UB      =   (irf+bb*nwse_lp(1:H));
irf_slp_baseline_LB     =   (irf_slp-bb*se_slp(1:H));
irf_slp_baseline_UB     =   (irf_slp+bb*se_slp(1:H));
irf_lp_zlb_baseline_LB  =   (irf_zlb-bb*nwse_lp_zlb(1:H));
irf_lp_zlb_baseline_UB  =   (irf_zlb+bb*nwse_lp_zlb(1:H));
irf_slp_zlb_baseline_LB =   (irf_slp_zlb-bb*se_slp_zlb(1:H));
irf_slp_zlb_baseline_UB =   (irf_slp_zlb+bb*se_slp_zlb(1:H));

xlswrite('Robustness.xlsx',irf,'Growth Specification','B2');
xlswrite('Robustness.xlsx',irf_zlb,'Growth Specification ZLB','B2');
xlswrite('Robustness.xlsx',irf_slp,'Growth Specification','C2');
xlswrite('Robustness.xlsx',irf_slp_zlb,'Growth Specification ZLB','C2');

%% Creating graph. 
     
t                       =   0:H-1;

if graph == 1
    figure
    plot(t,irf,'-k',t,irf_slp,'-b',t,irf_zlb,':k',t,irf_slp_zlb,':b','Linewidth',2)
    grid on 
    h = legend('LP','SLP','ZLB','ZLB SLP','Location','Best');
    set(h,'Interpreter','latex');
    title('Output','Interpreter','latex')
    xlabel('$h$','Interpreter','latex')

    figure
    plot(t,(1/100)*irf_slp,'-b',t,(1/100)*irf_slp_zlb,':b','Linewidth',2)
    grid on 
    h = legend('SLP','ZLB SLP','Location','Best');
    set(h,'Interpreter','latex');
    title('Output','Interpreter','latex')
    xlabel('$h$','Interpreter','latex')
end
